home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / actlib13.zip / TRACE.H < prev    next >
C/C++ Source or Header  |  1993-04-08  |  4KB  |  124 lines

  1. /***
  2.  *  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)
  3.  *
  4.  *  File    : trace.h
  5.  *
  6.  *  Description    : General macros for tracing
  7.  *                Print all the begining and ending of functions
  8.  *                (indented) with return values.
  9.  *
  10.  *  OS/Compiler : All
  11.  *
  12.  *  Usage       : - Add the line BEGIN( function_name, return_format );
  13.  *                  just after all the declaration of the function.
  14.  *                  return_format is one of the C standard format
  15.  *                  ( "%d", "%s", "%f", ... )
  16.  *
  17.  *                - Always put parenthesis after the 'return' statements
  18.  *                  ( ex: 'return(result);' )
  19.  *
  20.  *                - To trace a value call the function 'trace( (...) );'
  21.  *                  where (...) will be given as is to printf.
  22.  *
  23.  *                - Define 'TRACE' to actually perform tracing,
  24.  *                  do not define it to suppress tracing.
  25.  *
  26.  *                - You have to had somewhere int your code,
  27.  *                  outside of any scope:
  28.  *                     int G_nesting = 0;
  29.  *
  30.  ***/
  31.  
  32.  
  33. #ifndef _TRACE_H
  34. #define _TRACE_H
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38.  
  39.  
  40. #ifdef TRACE    /*  For debugging  */
  41.  
  42. extern int G_nesting;
  43.  
  44. /*   This has been removed, otherwise we cannot give a type to main like
  45.         int main(...)
  46.      We have to forget the 'int' type before it.
  47.  
  48. #define main \
  49.     extern int G_nesting = 0; \
  50.     main
  51. */
  52.  
  53.  
  54. #define trace( args ) \
  55.     printf( "%*d : ", G_nesting, G_nesting );    \
  56.     printf args
  57.  
  58. #define BEGIN( function, ret_format ) \
  59.     char _FUNCTION_[] = #function;                \
  60.     char _RET_FORMAT_[] = ret_format;              \
  61.     G_nesting ++;                                            \
  62.     printf( "\n" );                                \
  63.     trace( ("<<< Begin of function " #function "\n" ) );    \
  64.     trace( (" << in file " __FILE__ "\n") );        \
  65.     trace( (" << at line %d\n\n", __LINE__) )
  66.  
  67. #define exit( status ) \
  68.     { printf( "\n" );                        \
  69.       trace( (">>> Exit of function %s\n", _FUNCTION_) );        \
  70.       trace( (" >> in file " __FILE__ "\n") );            \
  71.       trace( (" >> at line %d\n\n", __LINE__) );            \
  72.       trace( (" >> exit code : '" #status "' = %d", status) );    \
  73.       G_nesting --;                                \
  74.       exit( status );                        \
  75.     }
  76.  
  77. #define _exit( status ) \
  78.     { printf( "\n" );                        \
  79.       trace( (">>> Exit of function %s\n", _FUNCTION_) );        \
  80.       trace( (" >> in file " __FILE__ "\n") );            \
  81.       trace( (" >> at line %d\n\n", __LINE__) );            \
  82.       trace( (" >> exit code : '" #status "' = %d", status) );    \
  83.       G_nesting --;                                \
  84.       _exit( status );                        \
  85.     }
  86.  
  87. #define return( value ) \
  88.     { char line[256];                            \
  89.       printf( "\n" );                            \
  90.       trace( (">>> Return from function %s\n", _FUNCTION_) );        \
  91.       trace( (" >> in file " __FILE__ "\n") );                \
  92.       trace( (" >> at line %d\n\n", __LINE__) );                \
  93.       sprintf( line, " >> return code : '" #value "' = %s\n\n", _RET_FORMAT_ ); \
  94.       trace( (line, value) );                        \
  95.       G_nesting --;                                    \
  96.       return( value );                            \
  97.     }
  98.  
  99. #define syserror( error ) \
  100.     { printf( "\n!!! %s\n", strerror(errno) );        \
  101.       printf( " !! in file     : " __FILE__ "\n" );            \
  102.       printf( " !! in function : %s\n", _FUNCTION_ );    \
  103.       printf( " !! at line     : %d\n", __LINE__ );            \
  104.       printf( " !! instruction : " #error "\n\n" );            \
  105.     }
  106.  
  107. #else
  108.  
  109. #define BEGIN( function, ret_format )
  110.  
  111. #define trace( args )
  112.  
  113. #define syserror( error ) \
  114.     { printf( "\n!!! %s\n", strerror(errno) );        \
  115.       printf( " !! in file     : " __FILE__ "\n" );            \
  116.       printf( " !! at line     : %d\n", __LINE__ );            \
  117.       printf( " !! instruction : " #error "\n\n" );            \
  118.     }
  119.  
  120. #endif
  121.  
  122.  
  123. #endif
  124.